#include<bits/stdc++.h>
using namespace std;
const int NN=1e6+4;
int a[NN],b[NN];
void solve()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=n;i++)
		if(!a[i]&&b[i])
		{
			puts("-1");
			return;
		}
	int last=0;
	long long ans=0;
	for(int i=1;i<=n;i++)
	{
		int w=max(0,a[i]-b[i]);
		ans+=max(0,w-last);
		if(!b[i])
			last=max(last,w);
		else
			last=w;
	}
	last=0;
	for(int i=1;i<=n;i++)
	{
		if(!b[i])
			continue;
		int w=max(0,b[i]-a[i]);
		ans+=max(0,w-last);
		last=w;
	}
	printf("%lld\n",ans);
}
int main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--)
		solve();
	return 0;
}
